<template>
  <div class="dashboard-container">
    <!-- 欢迎卡片 -->
    <el-card class="welcome-card">
      <div class="welcome-content">
        <div class="welcome-text">
          <h2>欢迎回来，{{ userStore.userInfo.nickname || '管理员' }}！</h2>
          <p>这是一个基于 Vue3 + Element Plus 的现代化管理后台系统</p>
          <div class="user-info">
            <span>用户名：{{ userStore.userInfo.username || 'admin' }}</span>
            <span>邮箱：{{ userStore.userInfo.email || 'admin@example.com' }}</span>
            <span>角色：
              <el-tag 
                v-for="role in userStore.roles" 
                :key="role" 
                :type="getRoleType(role)" 
                size="small"
                style="margin-left: 5px;"
              >
                {{ getRoleName(role) }}
              </el-tag>
            </span>
          </div>
        </div>
        <div class="welcome-icon">
          <el-icon size="60" color="#409eff"><Odometer /></el-icon>
        </div>
      </div>
    </el-card>

    <!-- 数据概览卡片 -->
    <div class="stats-grid">
      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-number">{{ stats.userCount }}</div>
            <div class="stat-label">用户总数</div>
            <div class="stat-subtitle">今日新增: {{ stats.todayUserCount }}</div>
          </div>
          <div class="stat-icon">
            <el-icon size="40" color="#67c23a"><User /></el-icon>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-number">{{ stats.articleCount }}</div>
            <div class="stat-label">文章总数</div>
            <div class="stat-subtitle">已发布: {{ stats.publishedArticleCount }} | 今日新增: {{ stats.todayArticleCount }}</div>
          </div>
          <div class="stat-icon">
            <el-icon size="40" color="#e6a23c"><Document /></el-icon>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-number">{{ stats.commentCount }}</div>
            <div class="stat-label">评论总数</div>
            <div class="stat-subtitle">今日新增: {{ stats.todayCommentCount }}</div>
          </div>
          <div class="stat-icon">
            <el-icon size="40" color="#f56c6c"><ChatDotRound /></el-icon>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-number">{{ stats.albumCount }}</div>
            <div class="stat-label">相册总数</div>
            <div class="stat-subtitle">照片: {{ stats.photoCount }}</div>
          </div>
          <div class="stat-icon">
            <el-icon size="40" color="#909399"><Picture /></el-icon>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-number">{{ stats.categoryCount }}</div>
            <div class="stat-label">分类总数</div>
          </div>
          <div class="stat-icon">
            <el-icon size="40" color="#409eff"><Folder /></el-icon>
          </div>
        </div>
      </el-card>

      <el-card class="stat-card">
        <div class="stat-content">
          <div class="stat-info">
            <div class="stat-number">{{ stats.tagCount }}</div>
            <div class="stat-label">标签总数</div>
          </div>
          <div class="stat-icon">
            <el-icon size="40" color="#9c27b0"><Collection /></el-icon>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 快捷操作 -->
    <el-card class="quick-actions">
      <template #header>
        <div class="card-header">
          <span>快捷操作</span>
        </div>
      </template>
      <div class="actions-grid">
        <div class="action-item">
          <el-button type="primary" @click="handleQuickAction('user')">
            <el-icon><User /></el-icon>
            用户管理
          </el-button>
          <el-tag 
            :type="configStatus.userRegisterEnabled ? 'success' : 'danger'" 
            size="small"
            style="margin-top: 5px;"
          >
            {{ configStatus.userRegisterEnabled ? '注册已启用' : '注册已禁用' }}
          </el-tag>
        </div>
        <div class="action-item">
          <el-button type="success" @click="handleQuickAction('article')">
            <el-icon><Document /></el-icon>
            文章管理
          </el-button>
          <el-tag 
            :type="configStatus.articlePublishEnabled ? 'success' : 'danger'" 
            size="small"
            style="margin-top: 5px;"
          >
            {{ configStatus.articlePublishEnabled ? '发布已启用' : '发布已禁用' }}
          </el-tag>
        </div>
        <div class="action-item">
          <el-button type="warning" @click="handleQuickAction('category')">
            <el-icon><Folder /></el-icon>
            分类管理
          </el-button>
          <el-tag 
            :type="configStatus.categoryCreateEnabled ? 'success' : 'danger'" 
            size="small"
            style="margin-top: 5px;"
          >
            {{ configStatus.categoryCreateEnabled ? '创建已启用' : '创建已禁用' }}
          </el-tag>
        </div>
        <div class="action-item">
          <el-button type="info" @click="handleQuickAction('tag')">
            <el-icon><Collection /></el-icon>
            标签管理
          </el-button>
          <el-tag 
            :type="configStatus.tagCreateEnabled ? 'success' : 'danger'" 
            size="small"
            style="margin-top: 5px;"
          >
            {{ configStatus.tagCreateEnabled ? '创建已启用' : '创建已禁用' }}
          </el-tag>
        </div>
        <div class="action-item">
          <el-button type="danger" @click="handleQuickAction('comment')">
            <el-icon><ChatDotRound /></el-icon>
            评论管理
          </el-button>
          <el-tag 
            :type="configStatus.commentPublishEnabled ? 'success' : 'danger'" 
            size="small"
            style="margin-top: 5px;"
          >
            {{ configStatus.commentPublishEnabled ? '发布已启用' : '发布已禁用' }}
          </el-tag>
        </div>
        <div class="action-item">
          <el-button @click="handleQuickAction('album')">
            <el-icon><Picture /></el-icon>
            相册管理
          </el-button>
          <el-tag 
            :type="configStatus.uploadEnabled ? 'success' : 'danger'" 
            size="small"
            style="margin-top: 5px;"
          >
            {{ configStatus.uploadEnabled ? '上传已启用' : '上传已禁用' }}
          </el-tag>
        </div>
      </div>
    </el-card>

    <!-- 系统信息 -->
    <el-card class="system-info">
      <template #header>
        <div class="card-header">
          <span>系统信息</span>
        </div>
      </template>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="系统名称">Spring Boot 3 管理后台</el-descriptions-item>
        <el-descriptions-item label="系统版本">1.0.0</el-descriptions-item>
        <el-descriptions-item label="前端框架">Vue 3 + Element Plus</el-descriptions-item>
        <el-descriptions-item label="后端框架">Spring Boot 3.0.2</el-descriptions-item>
        <el-descriptions-item label="数据库">MySQL 8.0</el-descriptions-item>
        <el-descriptions-item label="构建工具">Vite</el-descriptions-item>
      </el-descriptions>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Odometer, 
  User, 
  Document, 
  Folder, 
  Collection, 
  ChatDotRound, 
  Picture, 
  Setting 
} from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { getDashboardStats } from '@/api/dashboard'
import { getConfigStatus } from '@/api/system'

const router = useRouter()
const userStore = useUserStore()

// 统计数据
const stats = ref({
  userCount: 0,
  articleCount: 0,
  commentCount: 0,
  albumCount: 0,
  categoryCount: 0,
  tagCount: 0,
  photoCount: 0,
  publishedArticleCount: 0,
  todayUserCount: 0,
  todayArticleCount: 0,
  todayCommentCount: 0
})

// 配置状态
const configStatus = ref({
  articlePublishEnabled: true,
  commentPublishEnabled: true,
  userRegisterEnabled: true,
  categoryCreateEnabled: true,
  tagCreateEnabled: true,
  uploadEnabled: true
})

// 获取统计数据
const fetchStats = async () => {
  try {
    const response = await getDashboardStats()
    if (response.code === 200) {
      stats.value = response.data
    } else {
      ElMessage.error('获取统计数据失败')
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
    ElMessage.error('获取统计数据失败')
  }
}

// 获取配置状态
const fetchConfigStatus = async () => {
  try {
    const response = await getConfigStatus()
    if (response.code === 200) {
      configStatus.value = response.data
    } else {
      ElMessage.error('获取配置状态失败')
    }
  } catch (error) {
    console.error('获取配置状态失败:', error)
    ElMessage.error('获取配置状态失败')
  }
}

// 快捷操作处理
const handleQuickAction = (action) => {
  const actionMap = {
    user: '/user',
    article: '/article',
    category: '/category',
    tag: '/tag',
    comment: '/comment',
    album: '/album'
  }
  
  const path = actionMap[action]
  if (path) {
    router.push(path)
  } else {
    ElMessage.info('功能开发中...')
  }
}

// 获取角色类型
const getRoleType = (role) => {
  const roleTypes = {
    'ROLE_SUPER_ADMIN': 'danger',
    'ROLE_ADMIN': 'warning',
    'ROLE_USER': 'success',
    'ROLE_GUEST': 'info'
  }
  return roleTypes[role] || 'info'
}

// 获取角色名称
const getRoleName = (role) => {
  const roleNames = {
    'ROLE_SUPER_ADMIN': '系统管理员',
    'ROLE_ADMIN': '管理员',
    'ROLE_USER': '普通用户',
    'ROLE_GUEST': '游客'
  }
  return roleNames[role] || role
}

onMounted(() => {
  fetchStats()
  fetchConfigStatus()
})
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 20px;
  background: #f5f5f5;
  min-height: calc(100vh - 60px);
}

.welcome-card {
  margin-bottom: 20px;
  
  .welcome-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    
    .welcome-text {
      h2 {
        margin: 0 0 10px 0;
        color: #303133;
        font-size: 24px;
      }
      
      p {
        margin: 0 0 15px 0;
        color: #606266;
        font-size: 14px;
      }
      
      .user-info {
        display: flex;
        gap: 20px;
        font-size: 13px;
        color: #909399;
        
        span {
          display: flex;
          align-items: center;
          gap: 5px;
          
          &::before {
            content: '•';
            color: #409eff;
          }
        }
      }
    }
    
    .welcome-icon {
      display: flex;
      align-items: center;
    }
  }
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
  
  .stat-card {
    .stat-content {
      display: flex;
      align-items: center;
      justify-content: space-between;
      
      .stat-info {
        .stat-number {
          font-size: 32px;
          font-weight: bold;
          color: #303133;
          line-height: 1;
          margin-bottom: 8px;
        }
        
        .stat-label {
          font-size: 14px;
          color: #909399;
          margin-bottom: 4px;
        }
        
        .stat-subtitle {
          font-size: 12px;
          color: #c0c4cc;
        }
      }
      
      .stat-icon {
        display: flex;
        align-items: center;
      }
    }
  }
}

.quick-actions {
  margin-bottom: 20px;
  
  .card-header {
    font-weight: bold;
    color: #303133;
  }
  
  .actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 12px;
    
    .action-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 5px;
    }
    
    .el-button {
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
    }
  }
}

.system-info {
  .card-header {
    font-weight: bold;
    color: #303133;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .dashboard-container {
    padding: 10px;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }
  
  .actions-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .welcome-content {
    flex-direction: column;
    text-align: center;
    gap: 20px;
  }
  
  .user-info {
    flex-direction: column;
    gap: 10px;
    align-items: center;
  }
}
</style>
